<?php
/**
 * Timfan design模块处理程序
 *
 * @author Tim Fan
 * QQ:1026073477
 * @url http://i-fanr.com/
 */
defined('IN_IA') or exit('Access Denied');

class Tim_bookModuleSite extends WeModuleSite {
	public $table_book = 'tim_booksetting';
	public function doWebList() {
		global $_GPC, $_W;
		$uniacid = $_W['uniacid'];
		$pindex = max(1, intval($_GPC['page']));
		$psize = 20;
		$list = pdo_fetchall("SELECT * FROM ".tablename($this->table_book)." WHERE uniacid = '{$_W['uniacid']}' ORDER BY book_id DESC LIMIT " . ($pindex - 1) * $psize .',' .$psize);
		$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename($this->table_book) . " WHERE uniacid = '{$_W['uniacid']}'");
		$pager = pagination($total, $pindex, $psize);
		include $this->template('list');
	}

	
	public function doMobileMain() {
		global $_W,$_GPC;
		$uniacid = $_W['uniacid'];
		$book_id = intval($_GPC['book_id']);
		$allpic = pdo_fetchall("SELECT * FROM ". tablename('tim_book') ." WHERE book_id = :book_id ORDER BY `id` ASC", array(':book_id' => $book_id));
		$infoset = pdo_fetch("SELECT * FROM ". tablename('tim_booksetting') ." WHERE book_id = :book_id", array(':book_id' => $book_id));
		$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('tim_book') . " WHERE book_id =$book_id");
		if (!empty($allpic)) {
				include $this->template('index');
		}
	}


	public function doWebAdd_data() {
		//这个操作被定义用来呈现 管理中心导航菜单
		global $_W,$_GPC;
		$uniacid = $_W['uniacid'];
		$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
		$book_id = intval($_GPC['book_id']);
		load()->func('tpl');
		if('post' == $op){//添加或修改
			$id = intval($_GPC['id']);
			if(!empty($id)){
			$item = pdo_fetch("SELECT * FROM ".tablename('tim_book')." WHERE id=$id");
				empty($item)?message('亲,数据不存在！', '', 'error'):"";	
			}
			$info = pdo_fetch("SELECT * FROM ".tablename('tim_booksetting')." WHERE uniacid=$uniacid");
 			
			if(checksubmit('submit')){
				empty ($_GPC['ew_img'])?message('亲,此项不能为空'):$ew_img=$_GPC['ew_img'];
				$ew_intro =$_GPC['ew_intro'];
				$ew_title =$_GPC['ew_title'];
				$ew_order =$_GPC['ew_order'];
				$data = array(
					'id'=>$id,
					'book_id' =>$book_id,
					'ew_img' =>$ew_img,
					'ew_intro'=>$ew_intro,
					'ew_title'=>$ew_title,
					'ew_order'=>$ew_order,
				);
				
				if(empty($id)){
						pdo_insert('tim_book', $data);//添加数据
						message('数据添加成功！', $this->createWebUrl('add_data', array('op' => 'display','book_id' => $book_id)), 'success');
				}else{
						pdo_update('tim_book', $data, array('id' => $id));
						message('数据更新成功！', $this->createWebUrl('add_data', array('op' => 'display','book_id' => $book_id)), 'success');
				}
				
			}else{
				include $this->template('add_data');
			}
			
		}else if('del' == $op){//删除
		
			if(isset($_GPC['delete'])){
				$ids = implode(",",$_GPC['delete']);
				$sqls = "delete from  ".tablename('tim_book')."  where id in(".$ids.")"; 
				pdo_query($sqls);
				message('删除成功！', referer(), 'success');
			}
			$id = intval($_GPC['id']);
			$row = pdo_fetch("SELECT id FROM ".tablename('tim_book')." WHERE id = :id", array(':id' => $id));
			if (empty($row)) {
				//dump($_GPC);
				message('抱歉，数据不存在或是已经被删除！', $this->createWebUrl('add_data', array('op' => 'display')), 'error');
			}
			pdo_delete('tim_book', array('id' => $id));
			message('删除成功！', referer(), 'success');
			
		}else if('display' == $op){//显示
			$pindex = max(1, intval($_GPC['page']));
			$psize =20;//每页显示
			$condition = '';
			if (!empty($_GPC['keyword'])) {
				$condition .= " and question LIKE '%".$_GPC['keyword']."%'  ";
			}
			
			$list = pdo_fetchall("SELECT *  FROM ".tablename('tim_book')." WHERE book_id=$book_id $condition  ORDER BY id DESC LIMIT ".($pindex - 1) * $psize.','.$psize);//分页
			$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('tim_book')." WHERE book_id=$book_id $condition" );
			$pager = pagination($total, $pindex, $psize);
			include $this->template('add_data');
		}
		
	}

	
}